package com.yunmang.activity.statictis;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.activity.SearchEndActivity;
import com.yunmang.adapter.StaticticsSignAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.SavingStatictics;
import com.yunmang.entity.SignStatictics;
import com.yunmang.entity.event.EndEvent;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.DateUtil;
import com.yunmang.view.PopupWindows;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/23.
 */
public class SignedStatics extends BaseActivity {
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.tv_check)
    TextView tvCheck;
    @InjectView(R.id.listview)
    ListView listview;
    List<SignStatictics.ModelBean> dataList;
    private CustomDialog customDialog;
    private TextView btn_ok, btn_cancel,tvstart,tvend,sDate,eDate;
    private int EndDepartId=0,startid=0;//默认是0
    private StaticticsSignAdapter adapter;
    private Subscriber<SignStatictics> subscriber;
    @Override
    public int getLayoutId() {
        return R.layout.activity_statictics_sign;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        //注册
        EventBus.getDefault().register(this);
        initToolbar();
    }

    @Override
    public void initData() {
        dataList=new ArrayList<>();
        adapter=new StaticticsSignAdapter(context,dataList);
        listview.setAdapter(adapter);
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("已提统计");

        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu_one_text, menu);
        menu.getItem(0).setTitle("搜索");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.itemOne:
                customDialog = new CustomDialog(this, R.style.Prompt_dialog);
                if (!customDialog.isShowing()) {
                    customDialog.show();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @OnClick({R.id.tv_check})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_check:
                customDialog = new CustomDialog(this, R.style.Prompt_dialog);
                if (!customDialog.isShowing()) {
                    customDialog.show();
                }
                break;
        }
    }

    public class CustomDialog extends Dialog implements View.OnClickListener {

        public CustomDialog(Context context) {
            super(context);
        }

        public CustomDialog(Context context, int theme) {
            super(context, theme);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_sign);
            btn_cancel = (TextView) findViewById(R.id.btn_cancel);
            tvstart = (TextView) findViewById(R.id.tv_start);
            tvend= (TextView) findViewById(R.id.tv_end);
            sDate= (TextView) findViewById(R.id.tv_start_date);
            eDate= (TextView) findViewById(R.id.tv_end_date);
            btn_ok = (TextView) findViewById(R.id.btn_ok);
            tvstart.setOnClickListener(this);
            tvend.setOnClickListener(this);
            btn_ok.setOnClickListener(this);
            btn_cancel.setOnClickListener(this);
            sDate.setOnClickListener(this);
            eDate.setOnClickListener(this);
            this.setCancelable(true);
            sDate.setText(DateUtil.date2string(new Date(), DateUtil.YYYY_MM_DD));
            eDate.setText(DateUtil.date2string(new Date(), DateUtil.YYYY_MM_DD));
        }


        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_start:
                    if(AppConstant.type.equals("2")){
                        Intent in=new Intent(context, SearchEndActivity.class);
                        in.putExtra("start","start");
                        startActivityForResult(in,100);
                    }
                    break;
                case R.id.tv_end:
                    startActivity(new Intent(context, SearchEndActivity.class));
                    break;
                case R.id.btn_ok:

                    String start_date = sDate.getText().toString().trim();
                    String end_date = eDate.getText().toString().trim();
                    int result = end_date.compareTo(start_date);
                    if (result < 0) {
                        showToast("结束日期不能小于开始日期");
                        return;
                    }
                    dismiss();
                    getStatictics(start_date, end_date);
                    break;
                case R.id.tv_start_date:
                    PopupWindows startPop = new PopupWindows(context,sDate);
                    startPop.mHandler = new Handler() {
                        public void handleMessage(Message msg) {
                            switch (msg.what) {
                                case 1:
                                    Bundle bd = msg.getData();
                                    if ("".equals(bd.getString("date"))) {
                                        sDate.setText(DateUtil.date2string(new Date(), DateUtil.YYYY_MM_DD));
                                    } else {
                                        sDate.setText(bd.getString("date"));
                                    }
                                    break;
                            }
                        }
                    };
                    break;
                case R.id.tv_end_date:
                    PopupWindows endPop = new PopupWindows(context, eDate);
                    endPop.mHandler = new Handler() {
                        public void handleMessage(Message msg) {
                            switch (msg.what) {
                                case 1:
                                    Bundle bd = msg.getData();
                                    if ("".equals(bd.getString("date"))) {
                                        eDate.setText(DateUtil.date2string(new Date(), DateUtil.YYYY_MM_DD));
                                    } else {
                                        eDate.setText(bd.getString("date"));
                                    }
                                    break;
                            }
                        }
                    };
                    break;
                case R.id.btn_cancel:
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    private void getStatictics(String start_date, String end_date) {
        showWaitingDialog("数据加载中……");
        subscriber=new Subscriber<SignStatictics>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                showToast("获取失败");
                hideWaitingDialog();
            }

            @Override
            public void onNext(SignStatictics signStatictics) {
                hideWaitingDialog();
                if(signStatictics.getCode()==200){
                    if(signStatictics.getModel().size()>0){
                        Log.e("list",signStatictics.getModel().get(0).toString());
                        dataList=signStatictics.getModel();
                        adapter.refresh(dataList);
                    }else{
                        showToast("暂无数据");
                    }
                }else{
                    showToast("获取失败");
                }
            }


        };
        HttpUtils.getInstance().getSignStatictics(subscriber, AppConstant.userid,start_date,end_date,startid,EndDepartId);
        Log.e("userid",AppConstant.userid);
    }

    public void onEventMainThread(EndEvent event) {
        EndDepartId = event.getId();
        tvend.setText(event.getName());
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data!=null&&resultCode==110){
            startid=data.getIntExtra("id",0);
            tvstart.setText(data.getStringExtra("name"));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
